import { _decorator, Component, Node, EditBox, Label, find } from "cc";
import { CommonUtil } from "db://assets/Scripts/Utils/CommonUtil";
import { CheckOutEnum } from "db://assets/Scripts/Utils/CheckOutEnum";
import { StorageKit } from "db://assets/Scripts/Utils/StorageKit";
const { ccclass, property } = _decorator;

@ccclass("SaveBtnManager")
export class SaveBtnManager extends Component {
  commonUtil: CommonUtil;
  checkOutEnum: CheckOutEnum;
  enumData: any;

  constructor() {
    super();
    this.commonUtil = CommonUtil.getInstance();
    this.checkOutEnum = CheckOutEnum.getInstance();
    this.enumData = this.checkOutEnum.getEnumByType();
  }

  dealWithInput() {
    // 获取 HintLabel 组件
    const hintLabelPath = "Canvas/Container/RegisterPage/MainSplash/HintLabel";
    const hintLabelNode = find(hintLabelPath);
    const hintLabelComp = hintLabelNode.getComponent(Label);
    hintLabelComp.string = ""; // 初始化提示标签

    const editBoxNodePath = "Canvas/Container/RegisterPage/MainSplash/EditBoxTag";
    const editBoxNode = find(editBoxNodePath);

    const editBoxComp = editBoxNode.getComponent(EditBox);
    if (!editBoxComp.string) {
      hintLabelComp.string = this.enumData.PLS_INPUT_VAILD_NAME;
      return false;
    }

    console.log("YourNickName:", editBoxComp.string);
    this.registerUserData(editBoxComp.string);
    return true;
  }

  registerUserData(userNickName: string) {
    const registerTime = this.commonUtil.formatCurrentTime();
    const kit = this.node.addComponent(StorageKit);
    kit.storeUserData(userNickName, registerTime, registerTime);
  }
}
